<?php
namespace app\ucenter\controller;

use app\common\model\Cate;
use app\common\model\Navigation;
use app\common\model\User;
use app\common\model\UserToken;
use think\App;
use think\Controller;
use think\Db;

class Ucenter extends Controller
{
    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 分类列表
     */
    private $uid =1;
    protected function initialize()
    {
        parent::initialize();
        //判断有没有传递uid 没有默认为1
        if(empty(input('uid'))){
            $this->uid = 1;
        }else{
            $userTokenModel = new UserToken();
            $result = $userTokenModel->checkToken(input('uid'));
            if(!$result['code']){
                $this->uid = 1;
            }else{
                $this->uid = $result['data']['user_id'];
            }
        }
    }

    public function index()
    {
        //获取分类数据
        $cm = new Cate();
        $um = new User();
        $username =$um->where('id',$this->uid)->value('username');
        $cateinfo  = $cm->where('uid', $this->uid)->select()->toArray();
        $this->assign('uid', input('uid'));
        $this->assign('username', $username);
        $this->assign('cate_list',$cateinfo);
        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 导航列表
     */
    public function url(){
        //判断有没有传递uid 没有默认为1
        $cate_id = input('cate_id');
        $nm= new Navigation();
        $url_list =$nm->where('uid', $this->uid)->where('cate_name',$cate_id)->select()->toArray();
//        //获取分类数据
        $cm = new Cate();
        $cateinfo  = $cm->where('uid', $this->uid)->select()->toArray();
        foreach ($url_list as $kk=>&$vv){
            foreach ($cateinfo as $c=>$sk){
                if($vv['cate_name'] ==$sk['id']){
                    $vv['cate_name'] = $sk['cate_name'];
                }
            }
        }
        $this->assign('cate_id',$cate_id);
        $this->assign('cate_list',$cateinfo);
        $this->assign('uid', input('uid'));
        $this->assign('url_list',$url_list);
        return $this->fetch();
    }

    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改分类
     */
    public function m_cate(){
        $id = input('id');
        $cate_name = input('cate_name');
        $cm = new Cate();
        $cm->where('uid', $this->uid)->where('id',$id)->update(['cate_name'=>$cate_name]);
        header('Refresh:0;url=http://zwb8023.com/ucenter/ucenter/index?uid='. input('uid'));
    }

    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改排序
     */
    public function order(){
        $order = input('order');

        if(empty($order)){
            return  json_encode(showErr('没有可填项'));
        }else{
            $cm = new Cate();
            foreach ($order as $k=>$v){
                $cm->where('id',$k)->update(['sort'=>$v]);
            }
            header('Refresh:0;url=http://zwb8023.com/ucenter/ucenter/index?uid='.input('uid'));
        }

    }

    /**
     * @param string $name
     * @return string
     * 分类删除
     */
    public function delete(){
        $cate_id = input('cate_id');
        $nm = new Navigation();
        $ninfo = $nm->where('cate_name',$cate_id)->select();
        if($ninfo){
            return  showErr('该分类有收藏地址不可删除');
        }
        $cm = new Cate();
        $rerult = $cm->where('id',$cate_id)->delete();
        if($rerult){
            return  showOk(['id'=>$cate_id],'删除成功');
        }else{
            return  showErr('删除失败');
        }

    }

    /**
     * @param string $name
     * @return string
     * 修改导航
     */
    public function m_url(){
        $cate_id = input('cate_id');
        $url_id = input('url_id');
        $cate_name = input('page_name');
        $url = input('url');
//        $nm = new Navigation();
        Db::name('navigation')->where('cate_name',$cate_id)->where('id',$url_id)->update(['web_name'=>$cate_name,'web_url'=>$url]);
        header('Refresh:0;url=http://zwb8023.com/ucenter/ucenter/url?cate_id='.$cate_id);
    }

    /**
     * @param string $name
     * @return string
     * 删除导航
     */
    public function delete_url(){
        $id = input('id');
        $rerult = Db::name('navigation')->where('id',$id)->delete();
        if($rerult){
            return  showOk(['id'=>$id],'删除成功');
        }else{
            return  showErr('删除失败');
        }

    }
    /**
     * @param string $name
     * @return string
     * 修改用户导航
     */
    public function change_cate(){
        $id  = input('id');
        $to_id   = input('to_id');
        $cate_id = input('cate_id');
        $rerult = Db::name('navigation')->where('id',$id)->update(['cate_name'=>$to_id]);
        header('Refresh:0;url=http://zwb8023.com/ucenter/ucenter/url?cate_id='.$cate_id);
    }

    /**
     * @param string $name
     * @return string
     * 修改排序order
     */
    public function order_url(){
        $nm = new Navigation();
        $orderby = input('order');
        $cate_id = input('cate_id');
        foreach ($orderby as $k=>$v){
            $nm->where('id',$k)->update(['order_by'=>$v]);
        }
        header('Refresh:0;url=http://zwb8023.com/ucenter/ucenter/url?cate_id='.$cate_id);

    }
    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}
